package com.lujieni.aviator.controller;

import com.lujieni.aviator.entity.User;
import com.lujieni.aviator.service.AviatorExpressionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Aviator表达式控制器
 */
@RestController
@RequestMapping("/aviator")
public class AviatorController {

    @Autowired
    private AviatorExpressionService aviatorService;

    /**
     * 使用默认表达式检查用户匹配
     */
    @PostMapping("/check")
    public Map<String, Object> checkUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        boolean match = aviatorService.checkUserMatch(user);
        
        result.put("user", user);
        result.put("expression", aviatorService.getDefaultExpression());
        result.put("match", match);
        result.put("message", match ? "用户匹配条件" : "用户不匹配条件");
        
        return result;
    }

    /**
     * 使用自定义表达式检查用户匹配
     */
    @PostMapping("/check-custom")
    public Map<String, Object> checkUserWithCustomExpression(
            @RequestBody User user,
            @RequestParam String expression) {
        
        Map<String, Object> result = new HashMap<>();
        
        // 先验证表达式
        if (!aviatorService.validateExpression(expression)) {
            result.put("error", "表达式语法错误");
            result.put("expression", expression);
            return result;
        }
        
        boolean match = aviatorService.checkUserMatch(user, expression);
        
        result.put("user", user);
        result.put("expression", expression);
        result.put("match", match);
        result.put("message", match ? "用户匹配条件" : "用户不匹配条件");
        
        return result;
    }

    /**
     * 获取默认表达式
     */
    @GetMapping("/default-expression")
    public Map<String, Object> getDefaultExpression() {
        Map<String, Object> result = new HashMap<>();
        result.put("expression", aviatorService.getDefaultExpression());
        result.put("description", "默认表达式：name为lujieni且age大于30");
        return result;
    }

    /**
     * 验证表达式语法
     */
    @PostMapping("/validate")
    public Map<String, Object> validateExpression(@RequestParam String expression) {
        Map<String, Object> result = new HashMap<>();
        boolean valid = aviatorService.validateExpression(expression);
        
        result.put("expression", expression);
        result.put("valid", valid);
        result.put("message", valid ? "表达式语法正确" : "表达式语法错误");
        
        return result;
    }

    /**
     * Between表达式示例
     */
    @GetMapping("/between-examples")
    public Map<String, Object> getBetweenExamples() {
        Map<String, Object> result = new HashMap<>();
        Map<String, String> examples = new HashMap<>();
        
        examples.put("age-between-18-65", "age >= 18 && age <= 65");
        examples.put("age-between-25-50", "age >= 25 && age <= 50");
        examples.put("name-lujieni-age-between", "name == 'lujieni' && age >= 30 && age <= 60");
        examples.put("not-between", "!(age >= 30 && age <= 60)");
        examples.put("multiple-between", "(age >= 18 && age <= 25) || (age >= 60 && age <= 70)");
        
        result.put("examples", examples);
        result.put("description", "Between And 表达式示例");
        result.put("note", "Aviator中没有原生的between语法，需要使用 >= 和 <= 组合");
        
        return result;
    }

    /**
     * 测试Between表达式
     */
    @PostMapping("/test-between")
    public Map<String, Object> testBetweenExpression(
            @RequestBody User user,
            @RequestParam int minAge,
            @RequestParam int maxAge) {
        
        Map<String, Object> result = new HashMap<>();
        
        // 构建between表达式
        String expression = String.format("age >= %d && age <= %d", minAge, maxAge);
        
        boolean match = aviatorService.checkUserMatch(user, expression);
        
        result.put("user", user);
        result.put("expression", expression);
        result.put("minAge", minAge);
        result.put("maxAge", maxAge);
        result.put("match", match);
        result.put("message", match ? 
            String.format("用户年龄%d在%d到%d之间", user.getAge(), minAge, maxAge) : 
            String.format("用户年龄%d不在%d到%d之间", user.getAge(), minAge, maxAge));
        
        return result;
    }

    /**
     * 测试IN函数表达式
     */
    @PostMapping("/test-in")
    public Map<String, Object> testInExpression(
            @RequestBody User user,
            @RequestParam String values) {
        
        Map<String, Object> result = new HashMap<>();
        
        // 解析values参数，支持逗号分隔的值
        String[] valueArray = values.split(",");
        StringBuilder expressionBuilder = new StringBuilder("in(name");
        
        for (String value : valueArray) {
            expressionBuilder.append(", '").append(value.trim()).append("'");
        }
        expressionBuilder.append(")");
        
        String expression = expressionBuilder.toString();
        boolean match = aviatorService.checkUserMatch(user, expression);
        
        result.put("user", user);
        result.put("expression", expression);
        result.put("values", valueArray);
        result.put("match", match);
        result.put("message", match ? String.format("用户姓名'%s'在指定列表中", user.getName()) : String.format("用户姓名'%s'不在指定列表中", user.getName()));
        return result;
    }

    /**
     * IN函数示例
     */
    @GetMapping("/in-examples")
    public Map<String, Object> getInExamples() {
        Map<String, Object> result = new HashMap<>();
        Map<String, String> examples = new HashMap<>();
        
        examples.put("name-in-list", "in(name, 'lujieni', 'zhangsan', 'admin')");
        examples.put("age-in-list", "in(age, 25, 30, 35, 40)");
        examples.put("combined-in", "in(name, 'lujieni', 'admin') && in(age, 25, 28)");
        examples.put("not-in", "!in(name, 'guest', 'test')");
        examples.put("complex-in", "in(name, 'lujieni', 'zhangsan') || (in(age, 40, 45) && string.length(name) > 4)");
        
        result.put("examples", examples);
        result.put("description", "IN函数表达式示例");
        result.put("note", "使用自定义in()函数，语法: in(value, item1, item2, ...)");
        
        return result;
    }
}
